package com.pignest.hotel.ui.member;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Button;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.framework.util.CheckUtil;
import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Member;
import com.pignest.hotel.beans.MemberLevel;
import com.pignest.hotel.definition.MemberState;
import com.pignest.hotel.service.MemberService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Hotel;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.UIUtils;


@Deprecated
public class MemWindow extends Window implements EventListener {

	/**
	 * 会员新增与修改管理
	 */
	private static final long serialVersionUID = 1521087887829897950L;
	private MemberService memberService;
	private Member selMember;
	
	private String oPratorId;
	//private Member member;
	private MemberLevel level;
	private Textbox nameField;//会员姓名
	private Listbox typeListbox;//类型 如个人、公司、中介/旅行社
	private Listbox sexListbox;//性别
	private Textbox mobileField;//手机
	private Listbox certificateTypeListbox;//证件类型
	private Textbox certificateNoField;//证件号
	private Textbox mailField;//邮件
	//private Listbox registerModeListbox;//注册方式，现金购买、赠送
	private Listbox sendCardWayListbox;//发卡渠道,购买
	//private Textbox nationListbox;//国籍;
	//private Textbox provinceListbox;//省份
	private Textbox streetField;//街道;
	//private Textbox postField;//邮编;
	private Textbox cardNoField;//卡号
	private Listbox memberLevelListbox;//会员级别如金卡、银卡、普通
	private Datebox birthdayField;
	private Textbox cardPrice;
	private Label leveltip;
	private Label checktip;
	private Button submitbtn;
	private Textbox chargePrice;//需要支付
	private Listbox chargeTypeListbox;

	public MemWindow() {
		memberService = Services.getMemberService();
		addEventListener("onCreate", this);
	}
	
	@Override
	public void onEvent(Event event) throws Exception {
		// TODO Auto-generated method stub
		if ("onCreate".equals(event.getName())) {
			nameField=(Textbox)getFellow("nameField");
			typeListbox=(Listbox)getFellow("typeListbox");
			sexListbox=(Listbox)getFellow("sexListbox");
			mobileField=(Textbox)getFellow("mobileField");
			certificateTypeListbox=(Listbox)getFellow("certificateTypeListbox");
			certificateNoField=(Textbox)getFellow("certificateNoField");
			mailField=(Textbox)getFellow("mailField");
			//registerModeListbox=(Listbox)getFellow("registerModeListbox");
			sendCardWayListbox=(Listbox)getFellow("sendCardWayListbox");
			//nationListbox=(Textbox)getFellow("nationListbox");
			//provinceListbox=(Textbox)getFellow("provinceListbox");
			streetField=(Textbox)getFellow("streetField");
			//postField=(Textbox)getFellow("postField");
			cardNoField=(Textbox)getFellow("cardNoField");
			memberLevelListbox=(Listbox)getFellow("memberLevelListbox");
			birthdayField=(Datebox)getFellow("birthdayField");
			cardPrice =(Textbox)getFellow("cardPrice");
			chargePrice=(Textbox)getFellow("chargePrice");
			leveltip =(Label)getFellow("leveltip");
			checktip =(Label)getFellow("checktip");
			chargeTypeListbox=(Listbox)getFellow("chargeTypeListbox");
			submitbtn=(Button)getFellow("submitbtn");
			 changeLevelTip();
			 init();
		}
	}
	public void init()
	{
		if(this.selMember!=null){
			nameField.setValue(selMember.getName());
			nameField.setReadonly(true);
			UIUtils.setSelectedItem(sexListbox, selMember.getSex());
			UIUtils.setSelectedItem(typeListbox, selMember.getType());
			mobileField.setValue(selMember.getMobile());
			UIUtils.setSelectedItem(certificateTypeListbox,selMember.getCertificatType());
			certificateNoField.setValue(selMember.getCertificateNo());
			mailField.setValue(selMember.getMail());
			//registerModeListbox.setDisabled(true);
			//UIUtils.setSelectedItem(registerModeListbox,selMember.getRegisterMode());
			 
			birthdayField.setValue(selMember.getBirthday());
			UIUtils.setSelectedItem(sendCardWayListbox,selMember.getSendCardWay());
			 //UIUtils.setSelectedItem(nationListbox,member.getNation());
			memberLevelListbox.setDisabled(true);
			if(selMember.getMemberLevel()!=null){
			   UIUtils.setSelectedItem(memberLevelListbox, selMember.getMemberLevel().getMemberlevel());
			   
			 }
			// UIUtils.setSelectedItem(provinceListbox,member.getProvince());
			 streetField.setValue(selMember.getStreet());
			// postField.setValue(member.getPost());
			 cardNoField.setValue(selMember.getCardNo());
			 this.setoPratorId(selMember.getId());
		}else{
			this.setoPratorId(null);
		}
	}

	public String save(){
		try{
			 String name = nameField.getValue();
			 String cerNo =certificateNoField.getValue();
			 String chargeType=UIUtils.getSelectedValue(chargeTypeListbox);
			 if (StringUtils.isEmpty(name)) {
				return "会员姓名不能为空";
			 }
			 if(StringUtils.isEmpty(cerNo)){
				 return "证件号不能不空";
			 }
			 if(!Hotel.onDuty()){
				 //UIUtils.alert("当前不在值班状态不能操作");
				 return "当前不在值班状态不能操作";
			 }
			 Member m=null;
			
			 if (StringUtils.isEmpty(this.oPratorId)) {//新增
				 m = new Member();
			 }else
				 m=selMember;
			 
			 
			 m.setName(name);
			 m.setCardNo(cardNoField.getValue());
			 String sex=UIUtils.getSelectedValue(sexListbox);
		     m.setSex(sex);
		     m.setMobile(mobileField.getValue());
		     m.setCertificatType((String)UIUtils.getSelectedValue(certificateTypeListbox));
		     m.setCertificateNo(cerNo);
		     m.setType((String)UIUtils.getSelectedValue(typeListbox));
		     
		     m.setMail(mailField.getValue());
		     
		     m.setSendCardWay((String)UIUtils.getSelectedValue(sendCardWayListbox));
		    // member.setNation((String)UIUtils.getSelectedValue(nationListbox));
		    // member.setProvince((String)UIUtils.getSelectedValue(provinceListbox));
		     m.setStreet(streetField.getValue());
		    // member.setPost(postField.getValue());
		     //会员状态
		     m.setState(MemberState.OnUsed.name());
		     m.setBirthday(birthdayField.getValue());
		     m.setRegisterMode(chargeType);
		     int cPrice=0;
		     if(chargePrice.getValue()!=null)
		      cPrice =Utils.toPrice(chargePrice.getValue());
		     StringBuffer msg = new StringBuffer();
			    msg.append("你好!").append(name).append("\n");
			    
			   
			 if(StringUtils.isEmpty(this.oPratorId)){//新增
				 String s =memberService.ExistMember(m);
				 if (!s.equals("")){
				     return s;
			     }
				 int price =0;
				 m.setMemberLevel(this.level);
				// m.setRegisterMode((String)UIUtils.getSelectedValue(registerModeListbox));
				 m.setDealTime(new Date());
				 if(cardPrice.getValue()!=null)
					 price=Utils.toPrice(cardPrice.getValue());
				 if(price>cPrice){
						 chargePrice.focus();
						 return "充值不够卡金额！";
					 }
				 msg.append("会员级别：").append(m.getMemberLevel().getMemberlevel()).append("\n");
				 msg.append("卡金额：").append(Utils.toPrice(price)).append("\n");
				 msg.append("充值额:").append(Utils.toPrice(cPrice)).append("\n");
				 msg.append("合计:").append(Utils.toPrice(cPrice+price)).append("\n");
				 if(UIUtils.confirm(msg.toString())){
				    memberService.addMember(m,price,cPrice,chargeType);
				 }else{
					 return "取消!";
				 }
			 }else{
				 msg.append("会员级别：").append(m.getMemberLevel().getMemberlevel()).append("\n");
				 msg.append("充值额:").append(Utils.toPrice(cPrice)).append("\n");
				 msg.append("合计:").append(Utils.toPrice(cPrice)).append("\n");
				 if(UIUtils.confirm(msg.toString())){
				 memberService.updateMember(m,cPrice,chargeType);
				 }else{
					 return "取消!";
				 }
			 }
			return "";
		}catch(Exception e){
			e.printStackTrace();
			return e.getMessage();
		}
	}
	public void changeLevelTip(){
		
		String id=UIUtils.getSelectedValue(memberLevelListbox);
		MemberLevel level=memberService.getMemberLevel(id);
		this.level=level;
		//price = MoneyUtil.rand((MoneyUtil.cent2dollar(level.getLevelPrice())));
		String price=Utils.toPrice(level.getLevelPrice());
		String cardNeedPrice=Utils.toPrice(level.getLevelPay());
		//cardPrice.setValue(price);
		StringBuffer msg = new StringBuffer();
		msg.append("自助:(").append(level.getSelfhelplive()).append(")\r");
		msg.append("自助入住:(").append(level.getSelfhelplive()).append(")\r");
		msg.append("透支额度:(").append(Utils.toPrice(level.getOverdraft())).append(")\r");
		msg.append("需充值:(").append(price).append(")\r");
		msg.append("需支付:(").append(cardNeedPrice).append(")\r");
		msg.append("共需总额:(").append(Utils.toPrice(level.getLevelPrice()+level.getLevelPay())).append(")");
		cardPrice.setValue(cardNeedPrice);
		chargePrice.setValue(price);
		leveltip.setValue(msg.toString());
		
		
	}
/*	public void changeRegisterMode(){
		String model=UIUtils.getSelectedValue(registerModeListbox);
		if(model.equals("赠送")){
			cardPrice.setValue(0);
		}else{
			cardPrice.setValue(price);
		}
	}*/

    public void checkMobile(){
    	String value=mobileField.getValue();
    	
    	String s=memberService.ExistMemberSingle("mobile", value, "手机号", this.oPratorId);
    	if(!s.equals("")){
    		checktip.setValue(s);
    		submitbtn.setDisabled(true);
    	}else{
    		checktip.setValue("手机号有效");
    		submitbtn.setDisabled(false);
    	}
    	
    }
    public void checkMail(){
    	String value=mailField.getValue();
    	String s="";
    	if(!CheckUtil.chekcMail(value)&&!value.equals("")){
    		s="邮件格式不对";
    		submitbtn.setDisabled(true);
    	}else{
    		submitbtn.setDisabled(false);
    	}
    	 s=s+memberService.ExistMemberSingle("mail", value, "邮件", this.oPratorId);
    	if(!s.equals("")){
    		checktip.setValue(s);
    		submitbtn.setDisabled(true);
    	}else{
    		checktip.setValue("邮件有效");
    		submitbtn.setDisabled(false);
    	}
    	
    }
    public void checkCerNo(){
    	String value=certificateNoField.getValue();
    	String s="";
    	String cerType=(String)UIUtils.getSelectedValue(certificateTypeListbox);
    	if(cerType.equals("身份证")){
	    	if(!CheckUtil.validatePersonalId(value)&&!value.equals("")){
	    		s="身份证不对";
	    		submitbtn.setDisabled(true);
	    	}else{
	    		String brithday=CheckUtil.getBrithday(value);
	    		Date d=DateUtil.toDate(brithday);
	    		String sex =CheckUtil.getSex(value);
	    		System.out.println("sex:"+sex);
	    		UIUtils.setSelectedItem(sexListbox,sex);
	    		birthdayField.setValue(d);
	    		submitbtn.setDisabled(false);
	    	}
    	}
    	s= s+memberService.ExistMemberSingle("certificateNo", value, "证件号", this.oPratorId);
    	if(!s.equals("")){
    		checktip.setValue(s);
    		submitbtn.setDisabled(true);
    	}else{
    		checktip.setValue("证件号有效");
    		submitbtn.setDisabled(false);
    	}
    	
    }

	public Member getSelMember() {
		return selMember;
	}

	public void setSelMember(Member selMember) {
		this.selMember = selMember;
	}

	public String getoPratorId() {
		return oPratorId;
	}

	public void setoPratorId(String oPratorId) {
		this.oPratorId = oPratorId;
	}


    
}
